package com.xiaochong.tir.api.controller;

import akka.dispatch.OnFailure;
import akka.dispatch.OnSuccess;
import com.xc.logclient.utils.LogTrace;
import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.api.service.CurrencyDiagnosisService;
import com.xiaochong.tir.api.service.ExchangeInfoService;
import com.xiaochong.tir.api.service.TokenIndexRankService;
import com.xiaochong.tir.api.util.AkkaUtil;
import com.xiaochong.tir.common.data.hbasepo.CoinRank;
import com.xiaochong.tir.common.data.hbasepo.PopularityIndicator;
import com.xiaochong.tir.common.data.vo.ApiMarketHeatVo;
import com.xiaochong.tir.common.data.po.CurrencyInfo;
import com.xiaochong.tir.common.data.vo.ApiResultVo;
import com.xiaochong.tir.common.data.vo.AvgCostVo;
import com.xiaochong.tir.common.data.vo.SpaceTrendVo;
import com.xiaochong.tir.common.util.AkkaRouterUtils;
import com.xiaochong.tir.common.util.LogTraceKeysUtils;
import com.xiaochong.tir.common.util.Process;
import com.xiaochong.tir.common.util.RedisKeysUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.session.SessionProperties;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by org on 2018/5/12.
 * 接受命令处理页面数据，放入缓存中
 */
@Api(value = "接受数据处理结束接口")
@RestController
@RequestMapping("/apiservice/")
public class TimingHandleDataController {
    @Autowired
    private TokenIndexRankService tokenIndexRankService;
    @Autowired
    private CurrencyDiagnosisService currencyDiagnosisService;


    @Autowired
    private ExchangeInfoService exchangeInfoService;

    @Autowired
    private RedisServer redisServer;


    private void akkaProcess(String serviceName, Object o){
        /*System.out.println(LogTrace.getTrace().getTraceId());*/
        Process process = AkkaRouterUtils.getProcess(serviceName);
        /*Map<String,String> map = new HashMap<String,String>();
        map.put("traceId",LogTrace.getTrace().getTraceId());*/
        process.process(o, new OnSuccess() {
            @Override
            public void onSuccess(Object o) {
                LogTrace.info("service调用api的akka执行成功", "serviceToApi","service调用api的akka执行成功");
            }
        }, new OnFailure() {
            @Override
            public void onFailure(Throwable throwable) {
                Exception e = new Exception(throwable);
                e.printStackTrace();
                LogTrace.error("service调用api的akka执行错误"
                        , e);
            }
        },300);
    }
    /**
     * 市场表现组装数据存redis
     */
    @ApiOperation(value = "市场表现组装数据" , notes = "市场表现组装数据存redis(参数cmcId)")
    @GetMapping("getMarketPer")
    public ApiResultVo test() {
        ApiResultVo vo=new ApiResultVo();
        vo.setTradeId(LogTrace.getTrace().getTraceId());
        Process process = AkkaRouterUtils.getProcess( AkkaUtil.AKKAGETMARKET_PER);
        process.process( null, new OnSuccess() {
            @Override
            public void onSuccess(Object o) throws Throwable {
                LogTrace.info("service调用api的akka执行成功", "serviceToApi","service调用api的akka执行成功");

                /*String curentcyHigh=redisServer.get( RedisKeysUtils.MARkETPER_INDEX,CoinRank.class);
		        System.out.println(curentcyHigh);*/
                     }
        }, new OnFailure() {
            @Override
            public void onFailure(Throwable throwable) throws Throwable {
                Exception e = new Exception(throwable);
                e.printStackTrace();
                LogTrace.error("service调用api的akka执行错误"
                        , e);
            }
        } ,300);
        return vo;
    }

    /**
     * 市场表现组装数据存redis
     */
    @ApiOperation(value = "压力支撑组装数据" , notes = "压力支撑组装数据存redis")
    @GetMapping("getPressureSup")
    public ApiResultVo getPressureSup() {
        ApiResultVo vo=new ApiResultVo();
        vo.setTradeId(LogTrace.getTrace().getTraceId());
        Process process = AkkaRouterUtils.getProcess( AkkaUtil.PRESSURE_SUP);
        process.process( null, new OnSuccess() {
            @Override
            public void onSuccess(Object o) throws Throwable {
                LogTrace.info("service调用api的akka执行成功", "serviceToApi","service调用api的akka执行成功");
            }
        }, new OnFailure() {
            @Override
            public void onFailure(Throwable throwable) throws Throwable {
                Exception e = new Exception(throwable);
                e.printStackTrace();
                LogTrace.error("service调用api的akka执行错误"
                        , e);
            }
        } ,300);
        return vo;
    }

    /**
     * 多空趋势组装数据存redis
     */
    @ApiOperation(value = "多空趋势组装数据存redis" , notes = "多空趋势组装数据存redis")
    @GetMapping("getSpaceTrend")
    public ApiResultVo<List<SpaceTrendVo>> getSpaceTrend(){
        ApiResultVo vo=new ApiResultVo();
        vo.setTradeId(LogTrace.getTrace().getTraceId());
        Process process = AkkaRouterUtils.getProcess( AkkaUtil.SPACE_TREND);
        process.process( null, new OnSuccess() {
            @Override
            public void onSuccess(Object o) throws Throwable {
                LogTrace.info("service调用api的akka执行成功", "serviceToApi","service调用api的akka执行成功");
            }
        }, new OnFailure() {
            @Override
            public void onFailure(Throwable throwable) throws Throwable {
                Exception e = new Exception(throwable);
                e.printStackTrace();
                LogTrace.error("service调用api的akka执行错误"
                        , e);
            }
        } ,300);
        return vo;
    }

    /**
     * 平均成本组装数据存redis
     */
    @ApiOperation(value = "平均成本组装数据存redis" , notes = "平均成本组装数据存redis")
    @GetMapping("getAvgCost")
    public ApiResultVo<List<AvgCostVo>> getAvgCost() {
        ApiResultVo vo=new ApiResultVo();
        vo.setTradeId(LogTrace.getTrace().getTraceId());
        Process process = AkkaRouterUtils.getProcess( AkkaUtil.AVG_COST);
        process.process( null, new OnSuccess() {
            @Override
            public void onSuccess(Object o) throws Throwable {
                LogTrace.info("service调用api的akka执行成功", "serviceToApi","service调用api的akka执行成功");
            }
        }, new OnFailure() {
            @Override
            public void onFailure(Throwable throwable) throws Throwable {
                Exception e = new Exception(throwable);
                e.printStackTrace();
                LogTrace.error("service调用api的akka执行错误"
                        , e);
            }
        } ,300);
        return  vo;
    }

    /**
     * 平台币概念指数
     */
    @ApiOperation(value = "平台币概念指数" , notes = "平台币概念指数")
    @GetMapping("getBottomteZhi")
    public ApiResultVo<List<AvgCostVo>> getBottomteZhi() {
        ApiResultVo vo=new ApiResultVo();
        vo.setTradeId(LogTrace.getTrace().getTraceId());
        Process process = AkkaRouterUtils.getProcess( AkkaUtil.BOTTOMTE_ZHI);
        process.process( null, new OnSuccess() {
            @Override
            public void onSuccess(Object o) throws Throwable {

                LogTrace.info("service调用api的akka执行成功", "serviceToApi","service调用api的akka执行成功");
                String curentcyHigh=redisServer.get( RedisKeysUtils.BOTTOMTEZHI_INDEX);
                System.out.println(curentcyHigh);
            }
        }, new OnFailure() {
            @Override
            public void onFailure(Throwable throwable) throws Throwable {
                Exception e = new Exception(throwable);
                e.printStackTrace();
                LogTrace.error("service调用api的akka执行错误"
                        , e);
            }
        } ,300);
        return  vo;
    }

    /**
     * 货币诊断排名前5
     */
    @ApiOperation(value = "货币诊断排名前5" , notes = "货币诊断排名前5")
    @GetMapping("getMonetarydiagnosisTop5")
    public ApiResultVo<List<AvgCostVo>> getMonetarydiagnosisTop5() {
        ApiResultVo vo=new ApiResultVo();
        vo.setTradeId(LogTrace.getTrace().getTraceId());
        Process process = AkkaRouterUtils.getProcess( AkkaUtil.MONETARYDIAGNOSIS_TOP5);
        process.process( null, new OnSuccess() {
            @Override
            public void onSuccess(Object o) throws Throwable {
                LogTrace.info("service调用api的akka执行成功", "serviceToApi","service调用api的akka执行成功");
            }
        }, new OnFailure() {
            @Override
            public void onFailure(Throwable throwable) throws Throwable {
                Exception e = new Exception(throwable);
                e.printStackTrace();
                LogTrace.error("service调用api的akka执行错误"
                        , e);
            }
        } ,300);
        return  vo;
    }


    /**
     * 平台币种
     */
    @ApiOperation(value = "平台币种" , notes = "平台币种")
    @GetMapping("getConceptCurrency")
    public ApiResultVo<List<AvgCostVo>> getConceptCurrency() {
        ApiResultVo vo=new ApiResultVo();
        vo.setTradeId(LogTrace.getTrace().getTraceId());
        Process process = AkkaRouterUtils.getProcess( AkkaUtil.CONCEPT_CURRENCY);
        process.process( null, new OnSuccess() {
            @Override
            public void onSuccess(Object o) throws Throwable {
                LogTrace.info("service调用api的akka执行成功", "serviceToApi","service调用api的akka执行成功");
            }
        }, new OnFailure() {
            @Override
            public void onFailure(Throwable throwable) throws Throwable {
                Exception e = new Exception(throwable);
                e.printStackTrace();
                LogTrace.error("service调用api的akka执行错误"
                        , e);
            }
        } ,300);
        return  vo;
    }
    /**
     * 大盘指数
     * @return
     */
    @ApiOperation(value = "大盘指数", notes = "返回code：1000-成功；2000-无数据；5000-查询失败；")
    @GetMapping("/makeGrailIndexToCache")
    public ApiResultVo MakeGrailIndexToCache(){
        return tokenIndexRankService.MakeGrailIndexToCache();
    }

    @ApiOperation(value = "权重指数", notes = "返回code：1000-成功；2000-无数据；5000-查询失败；")
    @GetMapping("/makeWeightIndexToCache")
    public ApiResultVo MakeWeightIndexToCache(){
        return tokenIndexRankService.MakeWeightIndexToCache();
    }

    @ApiOperation(value = "行业指数", notes = "返回code：1000-成功；2000-无数据；5000-查询失败；")
    @GetMapping("/makeIndustryToCacheOf")
    public ApiResultVo MakeIndustryToCacheOf(){
        return tokenIndexRankService.MakeIndustryToCacheOf();
    }
    @ApiOperation(value = "赚钱效应", notes = "返回code：1000-成功；2000-无数据；5000-查询失败；")
    @GetMapping("/markMoneyEffectToCache")
    public ApiResultVo MarkMoneyEffectToCache(){
        return tokenIndexRankService.MarkMoneyEffectToCache();
    }
    @ApiOperation(value = "量能变化", notes = "返回code：1000-成功；2000-无数据；5000-查询失败；")
    @GetMapping("/marketInterpretationChangeToCache")
    public ApiResultVo MarketInterpretationChangeToCache(){
        return tokenIndexRankService.MarketInterpretationChangeToCache();
    }
    @ApiOperation(value = "权重表现", notes = "返回code：1000-成功；2000-无数据；5000-查询失败；")
    @GetMapping("/marketWeightsPerformanceToCache")
    public ApiResultVo MarketWeightsPerformanceToCache(){
        return tokenIndexRankService.MarketWeightsPerformanceToCache();
    }
    @ApiOperation(value = "市场热度/量化评估/货币流动性", notes = "返回code：1000-成功；2000-无数据；5000-查询失败；")
    @GetMapping("/marketHeatListToCache")
    public ApiResultVo MarketHeatListToCache(){
        return currencyDiagnosisService.MarketHeatListToCache();
    }
    @ApiOperation(value = "ar人气意愿", notes = "返回code：1000-成功；2000-无数据；5000-查询失败；")
    @GetMapping("/makePopularityARIndexData")
    public ApiResultVo makePopularityARIndexData(){
        return currencyDiagnosisService.makePopularityARIndexDataToCache();
    }
    /**
     * akka大盘指数/行业/权重
     * @return
     */
    @ApiOperation(value = "akka调用大盘指数/行业/权重", notes = "返回code：1000-成功；2000-无数据；5000-查询失败；")
    @GetMapping("/akkaMakeIndustryToCacheOf")
    public ApiResultVo akkaMakeIndustryToCacheOf(){
        ApiResultVo vo = new ApiResultVo();
        vo.setTradeId(LogTrace.getTrace().getTraceId());
        this.akkaProcess(AkkaUtil.AKKA_TOKEN_INDEX_RANK_GRAIL_FIVE,new HashMap<String,String>());
        return vo;
    }
    /**
     * akka大盘分析赚钱效应//权重表现/量能变化
     * @return
     */
    @ApiOperation(value = "akka调用大盘分析赚钱效应//权重表现/量能变化", notes = "返回code：1000-成功；2000-无数据；5000-查询失败；")
    @GetMapping("/akkaMakeGrailIndexToCache")
    public ApiResultVo akkaMakeGrailIndexToCache(){
        ApiResultVo vo = new ApiResultVo();
        vo.setTradeId(LogTrace.getTrace().getTraceId());
        this.akkaProcess(AkkaUtil.AKKA_TOKEN_INDEX_RANK_MARKET_FIVE,null);
        return vo;
    }
    /**
     * akka大盘分析赚钱效应//权重表现/量能变化
     * @return
     */
    @ApiOperation(value = "akk调用货币诊断市场热度、量能变化、货币流动性", notes = "返回code：1000-成功；2000-无数据；5000-查询失败；")
    @GetMapping("/akkaCurrencyDiagnosisAssessment")
    public ApiResultVo akkaCurrencyDiagnosisAssessment(){
        ApiResultVo vo = new ApiResultVo();
        vo.setTradeId(LogTrace.getTrace().getTraceId());
        this.akkaProcess(AkkaUtil.AKKA_CURRENCY_DIAGNOSIS_ASSESSMENT_FIVE,null);
        return vo;
    }

    /**
     * akka ar人气意愿指标
     * @return
     */
    @ApiOperation(value = "akka调用ar人气意愿", notes = "返回code：1000-成功；2000-无数据；5000-查询失败；")
    @GetMapping("/akkaMakePopularityARIndexData")
    public ApiResultVo akkaMakePopularityARIndexData(){
        ApiResultVo vo = new ApiResultVo();
        vo.setTradeId(LogTrace.getTrace().getTraceId());
        this.akkaProcess(AkkaUtil.AKKA_CURRENCY_DIAGNOSIS_POPULARITY_AR_FIVE,null);
        return vo;
    }
}
